#pragma once
#include <iostream>
#include <ctime>
#include <fstream>
#include <unistd.h>
#include <cstdarg>
#include <sys/stat.h>
#include <sys/types.h>

enum
{
    Debug = 0,
    Info,
    Warning,
    Error,
    Fatal
};
std::string LeveltoString(int level)
{
    switch (level)
    {
    case Debug:
        return "Debug";
    case Info:
        return "InFo";
    case Warning:
        return "Waring";
    case Error:
        return "Error";
    case Fatal:
        return "Fatal";
    default:
        return "unknow";
    }
}
enum
{
    screen = 10,
    File_one,
    classFile,
};

const int defaultstyle = screen;
const std::string default_failname = "log";
const std::string logdir = "log";

class Log
{
public:
    Log() : style(defaultstyle), filename(default_failname)
    {
        mkdir(logdir.c_str(),0775);
    }
    void Enable(int s)
    {
        style = s;
    }
    std::string TimeStamptoLocaltime()
    {
        time_t current = time(nullptr);
        struct tm *curr = localtime(&current);

        char time_buffer[1024];
        snprintf(time_buffer, sizeof(time_buffer), "%d-%d-%d %d:%d:%d", curr->tm_year + 1900, curr->tm_mon + 1, curr->tm_mday, curr->tm_hour, curr->tm_min, curr->tm_sec);
        return time_buffer;
    }
    void WriteLogClassFile(const std::string &levelstr, const std::string &message)
    {
        std::string logname = logdir;
        logname +="/";
        logname += filename;
        logname += levelstr;

        WriteLogOnefile(logname, message);
    }
    void WriteLogOnefile(const std::string &Logname, const std::string &message)
    {
        std::ofstream out(Logname, std::ios::app);
        if (!out.is_open())
            return;
        else
            out.write(message.c_str(), message.size());

        out.close();
    }
    void WriteLog(const std::string &levelstr, const std::string &message)
    {
        switch (style)
        {
        case screen:
            std::cout << message << std::endl;
            break;
        case File_one:
            WriteLogClassFile("all", message);
            break;
        case classFile:
            WriteLogClassFile(levelstr, message);
            break;
        default:
            break;
        }
    }
    void LogMessage(int level, const char *format, ...)
    {
        char rightbuffer[1024];
        va_list arg;
        va_start(arg, format); //...离可变参数最近的参数
        // arg 指向可变参数部分
        vsnprintf(rightbuffer, sizeof(rightbuffer), format, arg);
        va_end(arg);

        char leftbuffer[1024];
        std::string levelstr = LeveltoString(level);
        std::string time = TimeStamptoLocaltime();
        std::string idstr = std::to_string(getpid());
        snprintf(leftbuffer, sizeof(leftbuffer), "[%s][%s][%s]", levelstr.c_str(), time.c_str(), idstr.c_str());
        // printf("[%s][%s]%s\n",LeveltoString(level).c_str(),std::to_string(current).c_str(),left);
        // printf("%s %s\n",leftbuffer,rightbuffer);

        std::string loginfo = leftbuffer;
        loginfo += rightbuffer;
        WriteLog(levelstr, loginfo);
    }
    ~Log()
    {
    }

private:
    int style;
    std::string filename;
};


Log lg;
class Conf
{
public:
    Conf()
    {
        lg.Enable(screen);
    }
    ~Conf()
    {}
};
Conf conf;